let v = new Vue({
    el: '#app',
    data: function () {
        return {
            /*=============================首页分布图开始==========================*/
            DeptNumDoctor: [], //储存每个科室有多少医生
            tableIndexNoticeData: [],
            /*=============================首页分布图开始==========================*/

            /*==========================用户信息模块return开始=============================*/
            searchUsername: '',//查询输入框的账号
            searchStatus: '0',//查询输入框的状态
            searchNick: '',  //查询输入框的昵称
            tableUserData: [],
            totalUser: 0,//总条数
            pageSizeUser: 5,//每页5条
            currentPageUser: 1,//
            showRechargeDialog: false, // 控制充值模态框的显示与隐藏
            rechargeUserId: null, // 存储充值用户的 ID
            rechargeAmount: null, // 存储充值金额
            editedUser: {},//编辑用户对象
            editModalUserVisible: false, // 编辑模态框的显示状态
            editUserForm:{
                username:[
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    {pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,10}$/, message: '用户名必须是2~10长度的字符组成，且不允许使用标点符号', trigger: 'blur'}
                ],
                gender:[
                    {required: true, message: '性别不能为空', trigger: 'blur'}
                ],
                phone:[
                    {pattern:/^1[3456789]\d{9}$/, message: '请输入正确的手机号码'}
                ],
                email:[
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式错误', trigger: 'blur'}
                ],
                birthday:[
                    {required: true, message: '出生日期不能为空', trigger: 'blur'},
                ],
            },
            /*========================用户信息模块return结束===================================*/

            /*==========================医生模块return开始================================*/
            searchNo: '',//查询输入框的工号
            searchName: '',//查询输入框的username
            isCollapse: false,
            defaultOpeneds: ["1"], // 默认展开的菜单项
            selectedIndex: "1", //选中的菜单项的index值
            multipleSelection: [],//index=2,储存选择的条数
            tableDoctorData: [],
            tableDoctorMhData: [],
            searchData: [],//储存查询结果的数组
            total: 0,//总条数
            pageSize: 5,//每页5条
            currentPage: 1,//
            ids: [],
            departments: [], // 存储科室数据
            dialogDoctorVisible: false, // 控制增加模态框的可见性
            newDoctor: { // 包含新医生的数据
                no: '',
                username: '',
                password: '',
                gender: '',
                deptname: '',
                birthday: '',
                phone: '',
                email: '',
                degree: '',
                joinTime: '',
                deptId: ''
            },
            editedDoctor: {}, // 正在编辑的医生对象
            editModalVisible: false, // 编辑模态框的显示状态
            // 表单规则
            addDoctorRules: {
                no: [
                    {required: true, message: '请输入医生工号', trigger: 'blur'},
                    {pattern: /^D\d{4}$/, message: '工号必须D开头并且是5个长度的字符组成', trigger: 'blur'}
                ],
                username:[
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    {pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,10}$/, message: '用户名必须是2~10长度的字符组成，且不允许使用标点符号', trigger: 'blur'}
                ],
                gender:[
                    {required: true, message: '性别不能为空', trigger: 'blur'}
                ],
                phone:[
                    {required: true, message: '手机号不能为空', trigger: 'blur'},
                    {pattern:/^1[3456789]\d{9}$/, message: '请输入正确的手机号码'}
                ],
                email:[
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式错误', trigger: 'blur'}
                ],
                deptId:[
                    {required: true, message: '科室不能为空', trigger: 'blur'},
                ],
                birthday:[
                    {required: true, message: '出生日期不能为空', trigger: 'blur'},
                ],
                joinTime:[
                    {required: true, message: '入职时间不能为空', trigger: 'blur'},
                ]
            },
            editDoctorRules:{
                no: [
                    {required: true, message: '请输入医生工号', trigger: 'blur'},
                    {pattern: /^D\d{4}$/, message: '工号必须D开头并且是5个长度的字符组成', trigger: 'blur'}
                ],
                username:[
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    {pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,10}$/, message: '用户名必须是2~10长度的字符组成，且不允许使用标点符号', trigger: 'blur'}
                ],
                gender:[
                    {required: true, message: '性别不能为空', trigger: 'blur'}
                ],
                phone:[
                    {required: true, message: '手机号不能为空', trigger: 'blur'},
                    {pattern:/^1[3456789]\d{9}$/, message: '请输入正确的手机号码'}
                ],
                email:[
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式错误', trigger: 'blur'}
                ],
                deptId:[
                    {required: true, message: '科室不能为空', trigger: 'blur'},
                ],
                birthday:[
                    {required: true, message: '出生日期不能为空', trigger: 'blur'},
                ],
                joinTime:[
                    {required: true, message: '入职时间不能为空', trigger: 'blur'},
                ]
            },
            rules: {},
            /*==========================医生模块return结束=============================*/

            /*=========================科室信息模块return开始========================================*/
            tableDeptData: [],
            searchDeptName: '',//绑定查询输入框里面的值
            totalDept: 0,//总条数
            pageSizeDept: 5,//每页5条
            currentPageDept: 1,//
            totalDeptAndDoctor: 0,//总条数
            pageSizeDeptAndDoctor: 5,//每页5条
            currentPageDeptAndDoctor: 1,//
            totalDeptAndDoctorAppoint: 0,//总条数
            pageSizeDeptAndDoctorAppoint: 5,//每页5条
            currentPageDeptAndDoctorAppoint: 1,//
            editModalDeptVisible: false,
            editModalAppointVisible: false,
            newDept:{
                deptremark: ''
            },
            newAppoint:{
                schedulTime: '',
                sourceTotal: '',
                sourceSurplus: '',
                doctorNo: '',
            },
            editedDept: {},
            editedAppoint:{},
            editDeptRules:{
                deptremark:[
                    {required: true, message: '请输入科室备注', trigger: 'blur'},
                    {pattern: /^.{1,20}$/, message: "科室备注必须是1~20长度的字符组成'", trigger: 'blur'}
                ]
            },
            editAppointRules: {
                schedulTime: [
                    {required: true, message: '请输入排班日期', trigger: 'blur'},
                ],
                sourceTotal: [
                    {required: true, message: '请输入号源总量', trigger: 'blur'},
                ],
                sourceSurplus: [
                    {required: true, message: '请输入剩余号源', trigger: 'blur'},
                ]
            },
            showDoctorCard:false,
            showDeptCard:false,
            ListDoctor:[],
            currentDoctorId: -1, // 初始值为一个不存在的科室ID
            tableAppointData:[],//每天排班列表
            showAppointCard:false,
            currentDoctorName: '', // 当前医生的姓名
            currentDoctorNo: '',//当前医生的工号
            loading: false,
            showEmpty:true,
            no: '',
            username: '',
            deptId: '',
            searchDeptAppointDate: '',
            doctor:{
                currentDoctorName: '', // 当前医生的姓名
            },
            appointIds:[],
            newAppointRules:{
                schedulTime: [
                    {required: true, message: '请输入排班日期', trigger: 'blur'},
                ],
                sourceTotal: [
                    {required: true, message: '请输入号源总量', trigger: 'blur'},
                ],
                sourceSurplus: [
                    {required: true, message: '请输入剩余号源', trigger: 'blur'},
                ]
            },
            dialogAppointVisible: false,
            showSourceCard: false,
            showSource: false,
            AppointId: '',
            currentAppointId: -1, // 初始值为一个不存在的排班ID
            loadingSource: false,
            ListSource:[],
            newSource:{
                endTime: '',
                sourceSurplus: '',
                sourceTotal: '',
                schedulTime: '',
                appointId: ''
            },
            dialogSourceVisible: false,
            newSourceRules:{
                sourceTotal: [
                    {required: true, message: '请输入号源总量', trigger: 'blur'},
                ],
                sourceSurplus: [
                    {required: true, message: '请输入剩余号源', trigger: 'blur'},
                ]
            },
            AppointTotal: '',//当天排班的号源总量
            AppointSurplus: '',
            sourceTotal: 0,//从数据库累加的号源总量
            sourceSurplus: '',
            AppointStatus: '',
            /*=========================科室信息模块return结束========================================*/

            /*==========================药品管理模块return开始===========================================*/
            tableDrugData:[],
            drugNo: '',
            drugName: '',
            totalDrug: 0,//总条数
            pageSizeDrug: 5,//每页5条
            currentPageDrug: 1,//
            editedDrug: {},
            editModalDrugVisible: false,
            newDrug:{
                id: '',
                drugNo: '',
                drugName: '',
                inventory: '',
                unitprice: '',
                unit: '',
                usage: '',
                remark: ''
            },
            drugRules: {
                drugNo: [
                    {required: true, message: '药品编号不能为空', trigger: 'blur'},
                    {pattern: /^[A-Z]\d+$/, message: '药品编号必须是大写字母开头，后面跟数字', trigger: 'blur'}
                ],
                drugName: [
                    {required: true, message: '药品名称不能为空', trigger: 'blur'},
                ],
                inventory: [
                    {required: true, message: '库存不能为空', trigger: 'blur'},
                    {pattern: /^[1-9]\d*$/, message: '库存必须是正整数', trigger: 'blur'}
                ],
                unitprice: [
                    {required: true, message: '单价不能为空', trigger: 'blur'}
                ],
                unit: [
                    {required: true, message: '单位不能为空', trigger: 'blur'}
                ],
                usage: [
                    {required: true, message: '用法不能为空', trigger: 'blur'}
                ]
            },
            drugEditRules:{

            },
            dialogDrugVisible: false,
            /*==========================药品管理模块return结束===========================================*/

            /*========================公告信息模块return开始===================================*/
            tableNoticeData: [],//存储公告数据
            themeNotice: '',//公告主题字段---查询
            dialogNoticeVisible: false,//新增模态框关闭状态
            newNotice: {
                notice: '',
                issueTime: ''
            },
            totalNotice: 0,//总条数
            pageSizeNotice: 5,//每页5条
            currentPageNotice: 1,//
            editedNotice: {},
            editModalNoticeVisible: false,
            noticeRules:{
                notice:[
                    {required: true, message: '请输入主题', trigger: 'blur'},
                    {pattern: /^[a-zA-Z0-9_-]{4,20}$/, message: '用户名必须是4~20长度的字符组成', trigger: 'blur'}
                ],
                issueTime:[
                    {required: true, message: '发布日期不能为空', trigger: 'blur'},
                ]
            },
            /*==========================公告信息模块return结束=============================*/
        }
    },
    computed: {
        shouldShowAddButton() {
            // 遍历 ListSource 数组，检查每个元素的 status 属性
            for (let i = 0; i < this.ListSource.length; i++) {
                if (this.ListSource[i].status === 1) {
                    return false; // 如果有一个元素的状态为 1，则不显示新增按钮
                }
            }
            return true; // 如果所有元素的状态都不为 1，则显示新增按钮
        },
        switchValue() {
            const switchObj = {};
            this.tableDoctorData.forEach((doctor) => {
                switchObj[doctor.id] = doctor.status === 0;
            });
            return switchObj;
        },
        switchUserValue(){
            const switchUserObj = {};
            this.tableUserData.forEach((user) => {
                switchUserObj[user.id] = user.status === 0;
            });
            return switchUserObj;
        },
    },
    methods: {
        handleMenuSelect(index) {
            this.selectedIndex = index;
            if(index==='2'){ /*点击用户信息模块，会传过来相对应的index的值*/
                this.showUserInfo();/*自定义showDoctorInfo，跳转到这个方法，发送请求给后端查询数据，渲染页面上*/
            }
            if(index==='3'){ /*点击医生信息模块，会传过来相对应的index的值*/
                this.showDoctorInfo();/*自定义showDoctorInfo，跳转到这个方法，发送请求给后端查询数据，渲染页面上*/
            }
            if(index==='4'){
                this.showDrugInfo();
            }
            if (index==='10'){
                this.showNoticeInfo();
            }
            /*这里也是一样，写判断如果index==‘4’，查询相对应模块的index==4的所有信息*/
        },

        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
                });
            } else {
                this.clearSelection();
            }
        },
        clearSelection() {
            this.$refs.multipleTable.clearSelection();
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        /*=================================首页信息模块方法开始i========================================*/
        showIndexNotice(){
            axios.get("/pa/notice/selectIndexAll").then(response=>{
                v.tableIndexNoticeData = response.data.data;
            })
        },

        fetchData(){
            axios.get("/pa/dept/getDeptDoctor").then(response => {
                const departmentData = response.data.data; // 接收到的集合数据
                // 处理接收到的集合数据，例如更新状态或渲染图表等
                console.log(departmentData); // 输出集合数据
                v.DeptNumDoctor = departmentData;
                const data = departmentData.map(item =>({
                    name:item.deptname,
                    value:item.num
                }));
                const legendData = departmentData.map(item => item.deptname);
                const colorData = [
                    '#ff0000', // 红色
                    '#0000ff', // 蓝色
                    '#00ff00', // 绿色
                    '#ffff00', // 黄色
                    '#000000', // 黑色
                    '#ffa500', // 橙色
                    '#800080', // 紫色
                    '#ff00ff', // 粉色
                ];
                this.renderChart(data, legendData, colorData);
            })
        },
        renderChart(data, legendData, colorData){
            this.$nextTick(() => {
                this.chart = echarts.init(document.getElementById('chart-container'));

                const option = {
                    title: {
                        text: '科室人数统计',
                        left: 'center'
                    },
                    tooltip: {
                        formatter: function(params) {
                            const deptName = params.name;
                            const num = params.value;
                            return `${deptName}: ${num}人`;
                        }
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        top: '5%', // 调整整个图例的位置，向下移动10%
                        data: legendData,
                        formatter: function(name) {
                            const index = legendData.indexOf(name);
                            const color = colorData[index];
                            return `{color|${name}}`;
                        },
                        textStyle: {
                            rich: {
                                color: {
                                    height: 12,
                                    width: 12,
                                    borderRadius: 6,
                                    display: 'inline-block',
                                    marginRight: 5,
                                    /* marginTop: 40 // 控制颜色向下的偏移量*/
                                }
                            }
                        }
                    },
                    series: [
                        {
                            name: '人数',
                            type: 'pie',
                            radius: '55%',
                            data: data.map(item => ({
                                name: item.name,
                                value: item.value
                            })),
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            },
                            label: {
                                position: 'outside',
                                formatter: function(params) {
                                    return params.name;
                                }
                            }
                        }
                    ]
                };

                this.chart.setOption(option);
            });
        },

        renderIncomeChart(){
            axios.get("/pa/users/month").then(response=>{
                //map的形式出传过来了
                const dataMap = response.data.data;
                const months = Object.keys(dataMap);//获取传过来的key值
                const incomes = Object.values(dataMap);//获取传过来的value值
                const chartDom = document.getElementById('income-chart');
                const myChart = echarts.init(chartDom)
                const option = {
                    title: {
                        text: '---每月营业额统计图---', // 添加题目
                        textStyle: {
                            fontWeight: 'bold',
                            fontSize: 18
                        },
                        left: 'center',
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'line'
                        }
                    },
                    xAxis: {
                        type: 'category',
                        data: months
                    },
                    yAxis: {
                        type: 'value',
                        name: '收入'
                    },
                    series: [
                        {
                            data: incomes,
                            type: 'line',
                            smooth: true
                        }
                    ]
                };

                myChart.setOption(option);
            })
        },
        /*=================================首页信息模块方法结束=======================================*/

        /*===============================医生模块方法开始====================================*/
        deleteById(id){
            const currentPage = v.currentPage;
            axios.delete("/pa/doctor/deleteById/"+id).then(function (response) {
                if(response.data.code===1){
                    if (v.tableDoctorData.length === 1 && currentPage > 1) {
                        // 如果删除前是最后一页且只有一条数据，则返回上一页
                        v.$message.success("删除成功");
                        v.currentPage = currentPage - 1;
                        v.showDoctorInfo();//重新加载信息
                    }else {
                        v.$message.success("删除成功");
                        v.showDoctorInfo();//重新加载信息
                    }
                }else {
                    v.$message.error("删除失败")
                }
            })
        },
        handleSwitchChange(id){
            axios.put("/pa/doctor/changStatus/"+id).then(function (response) {
                console.log(response.data.data.status+"===")
                if(response.data.code==1){
                    if(response.data.data.status==1){
                        v.$message.error("禁用")
                        this.showDoctorInfo();
                    }else {
                        v.$message.success("启用")
                        this.showDoctorInfo();
                    }
                }
            }.bind(this))
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            v.pageSize = val; // 每次修改每页条数时，重置当前页数为第一页
            this.showDoctorInfo();
        },
        handleCurrentChange(page) {
            console.log("进来了jahhah")
            this.currentPage = page;
            this.showDoctorInfo();
        },
        showDoctorInfo() {//所有医生的基本信息
            console.log("哈哈啊啊哈哈")
            axios.get("/pa/doctor/selectAll?page=" + v.currentPage + "&pageSize=" + v.pageSize+"&no="+v.searchNo+"&username="+v.searchName).then(function (response) {
                if(response.data.code==1){
                    console.log(response.data.data.doctors+"===")
                    v.tableDoctorData = response.data.data.doctors;
                    v.total = response.data.data.total;
                }else {
                    v.$message.error("数据为空");
                }
            })
        },
        searchDoctors(){
            this.currentPage = 1;
            this.showDoctorInfo();
        },
        deleteSelection(){
            // 获取选中的医生信息的ID
            const selectedIds = this.multipleSelection.map((doctor) => doctor.id);
            if (selectedIds.length === 0) {
                this.$message.warning('请先选择要删除的医生信息');
                return;
            }
            v.ids = selectedIds;
            const currentPage = v.currentPage;
            // 弹出确认删除的对话框
            this.$confirm('确认删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                axios.delete("/pa/doctor/deleteByIds",{ data: v.ids }).then(response=> {//因为要重新加载医生信息，所以要回调，用=》
                    if(response.data.code===1){
                        if (v.tableDoctorData.length === 1 && currentPage > 1) {
                            // 如果删除前是最后一页且只有一条数据，则返回上一页
                            v.$message.success("删除成功");
                            v.currentPage = currentPage - 1;
                            v.showDoctorInfo();//重新加载信息
                        }else {
                            v.$message.success("删除成功");
                            v.showDoctorInfo();//重新加载信息
                        }
                    }else {
                        v.$message.error("删除失败");
                    }
                })
            }) .catch(() => {
                // 用户点击取消，不执行任何操作
            });
        },
        getDoctorById(id) {
            // 假设tableDoctorData是医生数据的数组
            return this.tableDoctorData.find(doctor => doctor.id === id);
        },
        editedById(id) {
            // 根据id获取医生对象，假设为getDoctorById方法
            const doctor = this.getDoctorById(id);
            if (doctor) {
                // 将当前行的数据复制到editedDoctor中
                this.editedDoctor = { ...doctor };
                console.log(v.editedDoctor.deptname+"====")
                this.editedDoctor.gender = String(doctor.gender); // 将整数类型转换为字符串类型
                this.editedDoctor.degree = String(doctor.degree);
                // 显示编辑模态框
                this.editModalVisible = true;
            }
        },
        submitEdit() {
            this.$refs.editForm.validate((valid) => {
                if(valid){
                    // 执行提交操作，例如更新医生数据
                    const updateDoctor ={
                        id:this.editedDoctor.id,
                        no:this.editedDoctor.no,
                        username:this.editedDoctor.username,
                        gender:this.editedDoctor.gender,
                        deptname:this.editedDoctor.deptname,
                        deptId:this.editedDoctor.deptId,
                        birthday:dayjs(this.editedDoctor.birthday).format('YYYY-MM-DD'),
                        phone:this.editedDoctor.phone,
                        email:this.editedDoctor.email,
                        degree:this.editedDoctor.degree,
                        joinTime:dayjs(this.editedDoctor.joinTime).format('YYYY-MM-DD'),
                        updateTime:dayjs(this.editedDoctor.updateTime).format('YYYY-MM-DD'),
                    }
                    /* v.deptId = v.deptname;//v.deptname里面装了deptId的值，赋值给他*/
                    v.editedDoctor = updateDoctor;
                    console.log(v.editedDoctor);
                    axios.put("/pa/doctor/updateById",v.editedDoctor).then(response=>{
                        if(response.data.code===1){
                            v.tableDoctorData = response.data.data;
                            v.$message.success("更新成功");
                            // 隐藏编辑模态框
                            this.editModalVisible = false;
                            this.showDoctorInfo();
                        }
                    }) .catch(error => {
                        console.error('医生数据更新失败', error);
                    });
                }
            })
        },
        resetForm() {
            this.$refs.editForm.resetFields();
        },

        /*模态框新增医生信息*/
        showAddDialog(){
            this.newDoctor = {
                no: '',
                username: '',
                gender: '',
                deptname: '',
                birthday: '',
                phone: '',
                email: '',
                degree: '',
                joinTime: ''
            };
            this.dialogDoctorVisible = true; // 将模态框的可见性设置为true
        },
        /*关闭模态框，把数据都清空*/
        resetNewDoctor() {
            console.log("你好")
            v.newDoctor = {
                no: '',
                username: '',
                gender: '',
                deptname: '',
                birthday: '',
                phone: '',
                email: '',
                degree: '',
                joinTime: ''
            };
            v.$refs.newDoctorForm.resetFields();
        },
        addDoctor() {
            this.$refs.newDoctorForm.validate((valid) => {
                if (valid) {
                    // 执行提交逻辑
                    axios.post("/pa/doctor/addDoctor",v.newDoctor).then(function (response) {
                        if(response.data.code===1){
                            v.$message.success("添加成功");
                            v.resetNewDoctor();
                            v.dialogDoctorVisible = false;
                            v.showDoctorInfo();
                        }else if(response.data.code===4001){
                            v.$message.error("工号已存在");
                        }
                    })
                }
            });
        },
        resetForm() {
            console.log("进来了")
            v.newDoctor = {
                no: '',
                username: '',
                gender: '',
                deptname: '',
                birthday: '',
                phone: '',
                email: '',
                degree: '',
                joinTime: ''
            };
        },
        getDepartments(){//获取科室数据
            axios.get("/pa/dept/selectDict").then(function (response) {
                if(response.data.code===1){
                    v.departments = response.data.data;
                }
            })
        },
        /*===============================医生模块方法结束====================================*/

        /*===============================用户信息模块方法开始====================================*/
        getStatusTagType(status){
            if(status===0){
                return '';
            }else if(status===1){
                return 'danger';
            }else if(status===2){
                return 'success';
            }
        },
        getStatusLabel(status){
            if (status === 0) {
                return '已预约';
            } else if (status === 1) {
                return '已取消';
            } else if (status === 2) {
                return '已就诊';
            }
        },
        resetUserForm(){
            this.$refs.editUserForm.resetFields();
        },
        //
        editedUserById(id){
            //根据id获取用户对象
            const user = this.getUserById(id);
            if(user){
                //将当前数据复制到editedUser中
                this.editedUser = {...user};
                this.editedUser.birthday = String(user.birthday);
                this.editedUser.gender = String(user.gender); // 将整数类型转换为字符串类型
                this.editModalUserVisible = true;
            }
        },
        getUserById(id){
            // tableUserData是医生数据的数组
            return this.tableUserData.find(user => user.id === id);
        },
        showRechargeModal(userId) {
            console.log(userId+"ppppp"+ userId);
            v.rechargeUserId = userId; // 将用户的 ID 存储起来
            v.showRechargeDialog = true; // 打开充值模态框
        },
        closeRechargeModal() {
            v.showRechargeDialog = false; // 关闭充值模态框
            v.rechargeUserId = null; // 清空充值用户的 ID
            v.rechargeAmount = null; // 重置充值金额
        },
        submitUserEdit(){
            this.$refs.editUserForm.validate((valid) => {
                if(valid){
                    // 执行提交操作，例如更新医生数据
                    const updateUser ={
                        id:this.editedUser.id,
                        username:this.editedUser.username,
                        gender:this.editedUser.gender,
                        birthday:dayjs(this.editedUser.birthday).format('YYYY-MM-DD'),
                        nickname:this.editedUser.nickname,
                        phone:this.editedUser.phone,
                        email:this.editedUser.email,
                        address:this.editedUser.address,
                        job:this.editedUser.job,
                        amount:this.editedUser.amount,
                        updateTime:dayjs(this.editedUser.updateTime).format('YYYY-MM-DD'),
                    }
                    /* v.deptId = v.deptname;//v.deptname里面装了deptId的值，赋值给他*/
                    v.editedUser = updateUser;
                    console.log(v.editedUser);
                    axios.put("/pa/users/updateById",v.editedUser).then(response=>{
                        if(response.data.code===1){
                            v.tableUserData = response.data.data;
                            v.$message.success("更新成功");
                            // 隐藏编辑模态框
                            this.editModalUserVisible = false;
                            this.showUserInfo();
                        }
                    }) .catch(error => {
                        console.error('用户数据更新失败', error);
                    });
                }
            })
        },

        performRecharge() {
            // 获取充值用户的 ID 和充值金额
            const userId = v.rechargeUserId;
            const amount = v.rechargeAmount;
            if(amount==null){
                this.$message.error('请输入充值金额');
                return;
            }
            // 根据用户的 ID 和充值金额进行相关的业务逻辑处理
            axios.get("/pa/users/topUp?id="+userId+"&amount="+amount).then(response=>{
                if(response.data.code==1){
                    v.$message.success("充值成功")
                    this.showUserInfo();
                    this.renderIncomeChart();//刷新月收入统计图
                }
            })
            // 处理完成后关闭充值模态框
            this.closeRechargeModal();
        },

        handleSwitchUserChange(id){
            axios.put("/pa/users/changStatus/"+id).then(function (response) {
                console.log(response.data.data.status+"===")
                if(response.data.code==1){
                    if(response.data.data.status==1){
                        v.$message.error("禁用")
                        this.showUserInfo();
                    }else {
                        v.$message.success("启用")
                        this.showUserInfo();
                    }
                }
            }.bind(this))
        },
        deleteUserById(id){
            const currentPageUser = v.currentPageUser; // 保存当前页码
            axios.delete("/pa/users/deleteById/"+id).then(function (response) {
                if(response.data.code===1){
                    if (v.tableUserData.length === 1 && currentPageUser > 1) {
                        // 如果删除前是最后一页且只有一条数据，则返回上一页
                        v.currentPageUser = currentPageUser - 1;
                        v.showUserInfo();//重新加载信息
                    }else {
                        v.showUserInfo();//重新加载信息
                    }

                    v.$message.success("删除成功")
                    v.showUserInfo();
                }else {
                    v.$message.error("删除失败")
                }
            })
        },

        handleSizeChangeUser(val) {
            console.log(`每页 ${val} 条`);
            v.pageSizeUser = val; // 每次修改每页条数时，重置当前页数为第一页
            this.showUserInfo();
        },
        handleCurrentChangeUser(page) {
            console.log("进来了jahhah")
            this.currentPageUser = page;
            this.showUserInfo();
        },
        showUserInfo(){//获取所有用户的信息
            console.log(v.searchStatus+"======")
            axios.get("/pa/users/selectAll?page="+v.currentPageUser+"&pageSize="+v.pageSizeUser+"&status="+v.searchStatus+"&username="+v.searchUsername+"&nickname="+v.searchNick).then(function (response) {
                if(response.data.code==1){
                    console.log(response.data.data.total+"======")
                    v.tableUserData = response.data.data.users;
                    v.totalUser = response.data.data.total;
                    if(v.searchStatus!=null){
                        v.searchStatus=""
                    }
                }else {
                    v.$message.error("数据为空");
                }
            })
        },
        searchUsers(){
            this.currentPage = 1;
            this.showUserInfo();
        },
        deleteUserSelection(){
            // 获取选中的用户信息的ID
            const selectedIds = this.multipleSelection.map((user) => user.id);
            if (selectedIds.length === 0) {
                this.$message.warning('请先选择要删除的用户信息');
                return;
            }
            v.ids = selectedIds;

            const currentPageUser = v.currentPageUser; // 保存当前页码
            // 弹出确认删除的对话框
            this.$confirm('确认删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                axios.delete("/pa/users/deleteByIds",{ data: v.ids }).then(response=> {//因为要重新加载用户信息，所以要回调，用=》
                    if(response.data.code===1){
                        v.$message.success("删除成功");
                        if (v.tableUserData.length === 1 && currentPageUser > 1) {
                            // 如果删除前是最后一页且只有一条数据，则返回上一页
                            v.currentPageUser = currentPageUser - 1;
                            v.showUserInfo();//重新加载信息
                        }else {
                            v.showUserInfo();//重新加载信息
                        }
                    }else {
                        v.$message.error("删除失败");
                    }
                })
            }) .catch(() => {
                // 用户点击取消，不执行任何操作
            });
        },
        /*===============================用户信息模块方法结束====================================*/

        /*==================================药品管理模块方法开始======================================*/
        handleSizeChangeDrug(val) {
            console.log(`每页 ${val} 条`);
            v.pageSizeDrug = val; // 每次修改每页条数时，重置当前页数为第一页
            this.showDrugInfo();
        },
        handleCurrentChangeDrug(page) {
            console.log("进来了jahhah")
            this.currentPageDrug = page;
            this.showDrugInfo();
        },
        searchDrug(){
            this.currentPageDrug = 1;
            this.showDrugInfo();
        },
        showDrugInfo() {
            console.log('进入药品信息')
            //所有药品的基本信息
            const drugLike={
                page:v.currentPageDrug,
                pageSize:v.pageSizeDrug,
                drugNo:v.drugNo,
                drugName:v.drugName
            }
            axios.post("/pa/drug/selectAll",drugLike).then(function (response) {
                if(response.data.code==1){
                    v.tableDrugData = response.data.data.drugs;
                    v.totalDrug = response.data.data.total;
                    v.drugNo = '';
                    v.drugName = '';
                }else {
                    v.$message.error("数据为空");
                }
            })
        },

        deleteDrugById(id){
            const currentPageDrug = v.currentPageDrug; //获取当前页
            axios.delete("/pa/drug/deleteDrugId/"+id).then(function (response) {
                if(response.data.code===1){
                    v.$message.success("删除成功")
                    if (v.tableDrugData.length === 1 && currentPageDrug > 1) {
                        // 如果删除前是最后一页且只有一条数据，则返回上一页
                        v.currentPageDrug = currentPageDrug - 1;
                        v.showDrugInfo();//重新加载信息
                    }else {
                        v.showDrugInfo();//重新加载信息
                    }
                }else {
                    v.$message.error("删除失败")
                }
            })
        },
        showAddDrug(){
            this.newDrug = {
                id: '',
                drugNo: '',
                drugName: '',
                inventory: '',
                unitprice: '',
                unit: '',
                usage: '',
                remark: ''
            };
            this.dialogDrugVisible = true; // 将模态框的可见性设置为true
        },
        /*关闭模态框，把数据都清空*/
        resetNewDrug() {
            v.newDrug = {
                id: '',
                drugNo: '',
                drugName: '',
                inventory: '',
                unitprice: '',
                unit: '',
                usage: '',
                remark: ''
            };
            v.$refs.newDrugForm.resetFields();
        },
        resetDrugForm(){//重置药品信息
            v.newDrug = {
                id: '',
                drugNo: '',
                drugName: '',
                inventory: '',
                unitprice: '',
                unit: '',
                usage: '',
                remark: ''
            };
        },
        addDrug() {
            this.$refs.newDrugForm.validate((valid) => {
                if (valid) {
                    // 执行提交逻辑
                    console.log("kakskajdk"+v.newDrug.drugName)
                    axios.post("/pa/drug/addDrug",v.newDrug).then(function (response) {
                        if(response.data.code===1){
                            v.$message.success("添加成功");
                            v.resetNewDrug();
                            v.dialogDrugVisible = false;
                            v.showDrugInfo();
                        }
                    })
                }
            });
        },
        getDrugById(id){
            // tableDrugData是公告数据的数组
            return this.tableDrugData.find(drug => drug.id === id);
        },
        editDurg(id){
            const drug = this.getDrugById(id);
            if(drug){
                this.editedDrug = {...drug};

                this.editModalDrugVisible = true;
            }
        },
        submitDrugEdit(){
            this.$refs.editDrugForm.validate((valid) => {
                if(valid){
                    const updateDrug = {
                        id:this.editedDrug.id,
                        drugNo:this.editedDrug.drugNo,
                        drugName:this.editedDrug.drugName,
                        inventory:this.editedDrug.inventory,
                        unitprice:this.editedDrug.unitprice,
                        unit:this.editedDrug.unit,
                        usage:this.editedDrug.usage,
                        remark:this.editedDrug.remark,
                    }
                    v.editedDrug = updateDrug;
                    axios.put("/pa/drug/updateById",v.editedDrug).then(response=>{
                        if(response.data.code===1){
                            v.tableDrugData = response.data.data;
                            v.$message.success("更新成功");
                            this.editModalDrugVisible = false;
                            this.showDrugInfo();
                        }
                    }).catch(error =>{
                        console.error('药品数据更新失败',error)
                    })
                }
            });
        },

        /*===============================公告信息模块方法开始====================================*/
        showNoticeInfo() {

            //所有公告的基本信息
            console.log(v.themeNotice+"====")
            axios.get("/pa/notice/selectAll?page="+v.currentPageNotice+"&pageSize="+v.pageSizeNotice+"&notice="+v.themeNotice).then(function (response) {
                if(response.data.code==1){
                    v.tableNoticeData = response.data.data.notices;
                    v.totalNotice = response.data.data.total;
                }else {
                    v.$message.error("数据为空");
                }
            })
        },
        searchNotice(){
            this.currentPage = 1;
            this.showNoticeInfo();
        },
        handleSizeChangeNotice(val) {
            console.log(`每页 ${val} 条`);
            v.pageSizeNotice = val; // 每次修改每页条数时，重置当前页数为第一页
            this.showNoticeInfo();
        },
        handleCurrentChangeNotice(page) {
            console.log("进来了jahhah")
            this.currentPageNotice = page;
            this.showNoticeInfo();
        },
        deleteNoticeSelection(){
            // 获取选中的医生信息的ID
            const selectedIds = this.multipleSelection.map((notice) => notice.id);
            if (selectedIds.length === 0) {
                this.$message.warning('请先选择要删除的公告信息');
                return;
            }
            v.ids = selectedIds;
            const currentPageNotice = v.currentPageNotice; //获取当前页
            // 弹出确认删除的对话框
            this.$confirm('确认删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                axios.delete("/pa/notice/deleteByIds",{ data: v.ids }).then(response=> {//因为要重新加载医生信息，所以要回调，用=》
                    if(response.data.code===1){
                        if (v.tableNoticeData.length === 1 && currentPageNotice > 1) {
                            // 如果删除前是最后一页且只有一条数据，则返回上一页
                            v.$message.success("删除成功");
                            v.currentPageNotice = currentPageNotice - 1;
                            v.showNoticeInfo();//重新加载信息
                        }else {
                            v.$message.success("删除成功");
                            v.showNoticeInfo();//重新加载信息
                        }
                    }else {
                        v.$message.error("删除失败");
                    }
                })
            }) .catch(() => {
                // 用户点击取消，不执行任何操作
            });
        },
        deleteNoticeById(id){
            const currentPageNotice = v.currentPageNotice; //获取当前页
            axios.delete("/pa/notice/deleteNoticeId/"+id).then(function (response) {
                if(response.data.code===1){
                    v.$message.success("删除成功")
                    if (v.tableNoticeData.length === 1 && currentPageNotice > 1) {
                        // 如果删除前是最后一页且只有一条数据，则返回上一页
                        v.currentPageNotice = currentPageNotice - 1;
                        v.showNoticeInfo();//重新加载信息
                        v.showIndexNotice();
                    }else {
                        v.showNoticeInfo();//重新加载信息
                        v.showIndexNotice();
                    }
                }else {
                    v.$message.error("删除失败")
                }
            })
        },
        submitNoticeEdit(){
            const updateNotice = {
                id:this.editedNotice.id,
                notice:this.editedNotice.notice
            }
            v.editedNotice = updateNotice;
            axios.put("/pa/notice/updateById",v.editedNotice).then(response=>{
                if(response.data.code===1){
                    v.tableNoticeData = response.data.data;
                    v.$message.success("更新成功");
                    this.editModalNoticeVisible = false;
                    this.showNoticeInfo();
                }
            }).catch(error =>{
                console.error('公告数据更新失败',error)
            })
        },
        showAddNotice(){
            this.newNotice = {
                id: '',
                notice: '',
                issueTime: '',
            };
            this.dialogNoticeVisible = true; // 将模态框的可见性设置为true
        },
        /*关闭模态框，把数据都清空*/
        resetNewNotice() {
            v.newNotice = {
                notice: '',
                issueTime: '',
            };
            v.$refs.newNoticeForm.resetFields();
        },
        editNotice(id){
            const notice = this.getNoticeById(id);
            if(notice){
                this.editedNotice = {...notice};
                this.editModalNoticeVisible = true;
            }
        },
        getNoticeById(id){
            // tableNoticeData是公告数据的数组
            return this.tableNoticeData.find(notice => notice.id === id);
        },
        addNotice() {
            this.$refs.newNoticeForm.validate((valid) => {
                if (valid) {
                    // 执行提交逻辑
                    axios.post("/pa/notice/addNotice",v.newNotice).then(function (response) {
                        if(response.data.code===1){
                            v.$message.success("添加成功");
                            v.resetNewNotice();
                            v.dialogNoticeVisible = false;
                            v.showNoticeInfo();
                            v.showIndexNotice();
                        }
                    })
                }
            });
        },
        resetNoticeForm(){//重置公告信息
            v.newNotice = {
                notice: '',
                issueTime: '',
            };
        },
        /*===============================公告信息模块方法结束====================================*/
    },

    mounted() {
        console.log("进来了")
        this.fetchData();
        this.renderIncomeChart();
        this.showIndexNotice();//公告信息模块
        this.getDepartments();//

    },

})